﻿using System.Windows;
using System.Windows.Interactivity;
using System.Windows.Controls;

namespace TheOliver.Controls.ApplicationFeatures
{
    public class MinimizeBehavior : Behavior<FrameworkElement>
    {
        protected override void OnAttached()
        {
            if (!Application.Current.IsRunningOutOfBrowser ||
                !Application.Current.HasElevatedPermissions)
            {
                this.AssociatedObject.Visibility = Visibility.Collapsed;
                return;
            }

            if (this.AssociatedObject is Button)
            {
                (this.AssociatedObject as Button).Click += (s, e) =>
                {
                    DoAction();
                };
            }
            else
            {
                this.AssociatedObject.MouseLeftButtonDown += (s, e) =>
                    {
                        DoAction();
                    };
            }
            base.OnAttached();
        }

        private static void DoAction()
        {
            Application.Current.MainWindow.WindowState = WindowState.Minimized;
        }

    }
}
